#ifndef _SCUDB_SYNC_HASH_TABLE_H_
#define _SCUDB_SYNC_HASH_TABLE_H_

#include "sd_global.h"
#include "sd_syncblist.h"
#include "sd_hashtable.h"

struct sd_MemoryAllocator;

typedef struct sd_SyncHashtable
{
	struct sd_MemoryAllocator* ma;
	sd_HashFunc hash_func;     /* The function to get the hash code from a key */
	sd_HashKeyCompare key_cmp; /* The function to compare two keys */
	size_t keysize;
	size_t elemsize;
	size_t hashsize;           /* The number of buckets */
	sd_SyncBlist buckets[1];
}sd_SyncHashtable;

typedef struct sd_SyncHashtableIterator
{
	sd_SyncHashtable* hashtable;
	int bucketIndex;
	sd_SyncBlistIterator bucketIter;
	bool bWLock;
}sd_SyncHashtableIterator;

sd_Ret sd_SyncHashtableCreate(sd_SyncHashtable** table, sd_HashFunc hash_func,sd_HashKeyCompare key_cmp,size_t hashsize, 
						  size_t bucketNodeSize, size_t keysize, size_t elemsize, struct sd_MemoryAllocator* ma);
sd_Ret sd_SyncHashtableFree(sd_SyncHashtable* table);
sd_Ret sd_SyncHashtableInsert(sd_SyncHashtable* table, const void* key, const void* elem);
sd_Ret sd_SyncHashtableLookup(sd_SyncHashtable* table, const void* key, void* elem);
sd_Ret sd_SyncHashtableLookupWithLock(sd_SyncHashtable* table, const void* key, sd_SyncHashtableIterator* iter, bool bLockList);
sd_Ret sd_SyncHashtableInsertWithLock(sd_SyncHashtableIterator* iter, const void* key, const void* elem);
sd_Ret sd_SyncHashtableReleaseLock(sd_SyncHashtableIterator* iter);
sd_Ret sd_SyncHashtableUpdate(sd_SyncHashtable* table, const void* key, const void* elem);
sd_Ret sd_SyncHashtableDelete(sd_SyncHashtable* table, const void* key);
sd_Ret sd_SyncHashtableRemoveAndReleaseLock(sd_SyncHashtableIterator* iter);
sd_Ret sd_SyncHashtableClear(sd_SyncHashtable* table);
sd_Ret sd_SyncHashtableBegin(sd_SyncHashtable* table, sd_SyncHashtableIterator* iter, bool bWLock);
sd_Ret sd_SyncHashtableNext(sd_SyncHashtableIterator* iter);
sd_Ret sd_SyncHashtableGet(sd_SyncHashtableIterator* iter, void* elem);
sd_Ret sd_SyncHashtableGetKey(sd_SyncHashtableIterator* iter, void* elem);


#endif
